package fun.rockchen.dao;

import fun.rockchen.bean.AddTaskEntity;
import fun.rockchen.bean.MapTaskEntity;
import fun.rockchen.bean.Task;
import fun.rockchen.bean.TaskTimeEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Rock Chen
 * @Description:
 * @date 2023/10/16 11:30
 */

@Mapper
public interface TaskDao {
    @Select("""
                select * from task
                inner join userMapTask uMT on task.id = uMT.taskId
            where userId = '${userId}' and startTime like concat('%','${startTime}','%')""")
    List<Task> getTaskByStartTime(@Param("userId") String userId, @Param("startTime") String startTime);

    @Insert("insert into task (id, taskContent, startTime, endTime, status, type, description) values (#{id}, #{taskContent}, #{startTime}, #{endTime}, #{status}, #{type}, #{description})")
    boolean addTask(AddTaskEntity addTaskEntity);

    @Insert("insert into userMapTask (userId, taskId) values ('${userId}', '${id}')")
    boolean addUserMapTask(AddTaskEntity addTaskEntity);

    @Insert("insert into taskMapTag (taskId, tagId) values('${taskId}','${tagId}')")
    boolean addTaskMapTag(@Param("taskId") String taskId, @Param("tagId") String tagId);

    @Update("update task set taskContent = '${taskContent}', startTime = '${startTime}', endTime = '${endTime}', status = '${status}', type = '${type}', description = '${description}' where id = #{id}")
    boolean updateTask(Task task);

    @Delete("delete from taskMapTag where taskId = '${taskId}'")
    boolean updateTaskTagStep1(String taskId);

    @Insert("insert into taskMapTag (taskId, tagId) values ('${id}', (select id from tag" +
            "    inner join userMapTag on tag.id = userMapTag.tagId " +
            "where tag.id = '${tagId}' and userId = '${userId}'))")
    boolean updateTaskTagStep2(Task task);

    @Delete("delete taskMapTag, userMapTask " +
            "from taskMapTag " +
            "    inner join userMapTask on userMapTask.taskId = taskMapTag.taskId " +
            "where taskMapTag.taskId = '${id}'")
    boolean deleteTaskStep1(String id);

    @Delete("delete from task where id = '${id}'")
    boolean deleteTaskStep2(String id);

    @Update("update task set status = 2 where id = '${id}' ")
    boolean changeTaskIntoCancel(MapTaskEntity mapTaskEntity);

    @Update("update task set status = 3 where id = '${id}'")
    boolean changeTaskIntoPending(MapTaskEntity mapTaskEntity);

    @Select("select status, count(*) as num from task inner join userMapTask on task.id = userMapTask.taskId where userMapTask.userId = #{id} group by status")
    List<Task> getStatusTask(MapTaskEntity mapTaskEntity);

    @Select("""
            select task.id, taskContent, startTime, endTime, status, type, description, GROUP_CONCAT(tagContent separator ',') as tag from task
                inner join userMapTask on task.id = userMapTask.taskId
                inner join taskMapTag on task.id = taskMapTag.taskId
                inner join tag on taskMapTag.tagId = tag.id
            where userMapTask.userId = '${id}'
              and to_days(startTime) = to_days('${options}')
            group by task.id, taskContent, startTime, endTime, status, type, description
            order by startTime;""")
    List<Task> getTaskByTime(MapTaskEntity mapTaskEntity);

    @Select("""
            select task.id, taskContent, startTime, endTime, status, type, description, GROUP_CONCAT(tagContent separator ',') as tag from task
                inner join userMapTask on task.id = userMapTask.taskId
                inner join taskMapTag on task.id = taskMapTag.taskId
                inner join tag on taskMapTag.tagId = tag.id
            where userMapTask.userId = '${id}' and status = ${options}
            group by task.id, taskContent, startTime, endTime, status, type, description;""")
    List<Task> getTaskByState(MapTaskEntity mapTaskEntity);

    @Select("""
            select task.id, taskContent, startTime, endTime, status, type, description, GROUP_CONCAT(tagContent separator ',') as tag
            from task
                inner join userMapTask on task.id = userMapTask.taskId
                inner join taskMapTag on task.id = taskMapTag.taskId
                inner join tag on taskMapTag.tagId = tag.id
            where task.id = '${id}'
            group by task.id, taskContent, startTime, endTime, status, type, description;""")
    Task getTaskById(String id);

    @Select("""
            select DATE_FORMAT(startTime,'%Y-%m-%d') data
            from task
                inner join userMapTask on task.id = userMapTask.taskId
                inner join taskMapTag on task.id = taskMapTag.taskId
                inner join tag on taskMapTag.tagId = tag.id
            where userMapTask.userId = '${id}'
              and taskContent like concat('%','${keyword}','%')
              and status = '${status}'
              and tagContent like concat('%','${tag}','%')
              and startTime like concat('%','${options}','%')
            group by data
            order by data asc;""")
    List<String> getTimeByDate(MapTaskEntity mapTaskEntity);

    @Select("""
            select task.id, taskContent, startTime, endTime, status, type, GROUP_CONCAT(tagContent separator ',') as tag
            from task
                inner join userMapTask on task.id = userMapTask.taskId
                inner join taskMapTag on task.id = taskMapTag.taskId
                inner join tag on taskMapTag.tagId = tag.id
            where userMapTask.userId = '${id}'
              and taskContent like concat('%','${keyword}','%')
              and (
              select GROUP_CONCAT(tagContent separator ',') as tag
              from task
                inner join userMapTask on task.id = userMapTask.taskId
                inner join taskMapTag on task.id = taskMapTag.taskId
                inner join tag on taskMapTag.tagId = tag.id) like concat('%','${tag}', '%')
              and status = '${status}'
              and startTime like concat('%','${options}','%')
            group by task.id, taskContent, startTime, endTime, status, type, description;""")
    List<Task> getTaskByDate(MapTaskEntity mapTaskEntity);

    @Select("select coalesce(SUM(sec_diff), 0) from (SELECT TIMESTAMPDIFF(SECOND, startTime, endTime) AS sec_diff\n" +
            "FROM task\n" +
            "inner join userMapTask uMT on task.id = uMT.taskId\n" +
            "where startTime like concat('%','${startTime}','%') and userId = '${userId}') as tsd")
    long getTaskTotalTime(TaskTimeEntity taskTimeEntity);
}
